package priv.conceit.sc.hoxton.file.handle.compress;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.springframework.stereotype.Component;
import priv.conceit.sc.hoxton.common.entity.file.dto.FilePathDTO;
import priv.conceit.sc.hoxton.file.util.PdfUtil;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * sc-hoxton
 *
 * @author Conceit
 * 2020/6/12 增加方法
 * @since 1.0.0, 2020/6/12
 */
@Component
public class TarCompressHandle extends AbsCompressHandle{


	/**
	 * unCompress
	 *
	 * @param filePathDTO 封装的文件信息
	 * @param inputStream 文件输入流
	 * @param convertFile 转换过之后的文件
	 * @return boolean
	 * @author conceit
	 * @date 2020/6/15 17:12
	 */
	@Override
	public boolean unCompress(FilePathDTO filePathDTO, InputStream inputStream, File convertFile) {
		TarArchiveInputStream tar=new TarArchiveInputStream(inputStream);

		List<String> items = new ArrayList<>();
		try {
			while (Objects.nonNull(tar.getNextTarEntry())){
				TarArchiveEntry tarArchiveEntry=tar.getCurrentEntry();
				items.add(tarArchiveEntry.getName());
			}
			PdfUtil pdfUtil=new PdfUtil();
			pdfUtil.generatePdf(convertFile,filePathDTO.getOriginalName(),items);
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}finally {
			try {
				tar.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return true;

	}
}
